/****************************************************************************
 * Copyright (c) 2010 Torkild U. Resheim
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Torkild U. Resheim  - initial API and implementation
 *****************************************************************************/
package no.resheim.buildmonitor.hudson.ui;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.ecf.ui.SharedImages;
import org.eclipse.ecf.ui.util.PasswordCacheHelper;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * 
 * @author Torkild Ulvøy Resheim
 * @since 1.0
 */
public class ConnectWizardPage extends WizardPage {

	private Combo url;
	private Text login;
	private Text password;

	protected ConnectWizardPage(String pageName) {
		super(pageName);
		setTitle("Hudson Connection Wizard");
		setDescription("Specify a Hudson URL to connect to. Add username and password if the server has been secured.");
		setPageComplete(false);
		setImageDescriptor(SharedImages
				.getImageDescriptor(SharedImages.IMG_CHAT_WIZARD));
	}

	@Override
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout(2, false);
		container.setLayout(layout);
		GridData gd = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
		Label urlLabel = new Label(container, SWT.LEAD);
		urlLabel.setText("URL:");
		url = new Combo(container, SWT.BORDER);
		url.setLayoutData(gd);
		url.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				verify();
			}
		});
		url.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent e) {
				verify();
			}

			public void widgetSelected(SelectionEvent e) {
				verify();
			}
		});
		// Allow user to select predefined instances.
		url.add("http://localhost:8080/");
		url.add("https://build.eclipse.org/hudson/");
		url.add("http://deadlock.netbeans.org/hudson/");
		url.add("http://norvm-tools-m.norway.atmel.com/hudson");
		url.select(0);
		Label userLabel = new Label(container, SWT.LEAD);
		userLabel.setText("Login:");
		login = new Text(container, SWT.BORDER);
		login.setLayoutData(gd);
		login.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				verify();
			}
		});
		login.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent e) {
				verify();
			}

			public void widgetSelected(SelectionEvent e) {
				verify();
			}
		});
		Label pwdLabel = new Label(container, SWT.LEAD);
		pwdLabel.setText("Password:");
		password = new Text(container, SWT.BORDER | SWT.PASSWORD);
		password.setLayoutData(gd);
		password.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				verify();
			}
		});
		password.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent e) {
				verify();
			}

			public void widgetSelected(SelectionEvent e) {
				verify();
			}
		});
		setControl(container);
	}

	public String getURL() {
		return url.getText();
	}

	public String getLogin() {
		return login.getText();
	}

	public String getPassword() {
		return password.getText();
	}

	static Pattern serverPattern = Pattern
			.compile("http(s)?://([-a-z0-9]+(\\.[-a-z0-9]+)*)?");

	protected void updateStatus(String message) {
		setErrorMessage(message);
		setPageComplete(message == null);
	}

	protected void restorePassword(String username) {
		final PasswordCacheHelper pwStorage = new PasswordCacheHelper(username);
		final String pw = pwStorage.retrievePassword();
		if (pw != null) {
			password.setText(pw);
			password.setSelection(0, pw.length());
		}
	}

	private void verify() {
		final String text = url.getText();
		if (text.equals("")) { //$NON-NLS-1$
			updateStatus("URL to the Hudson build server must be specified");
		} else {
			final Matcher hostMatcher = serverPattern.matcher(text);
			if (!hostMatcher.matches()) {
				updateStatus("Format of the build server URL is not correct.");
			}
			restorePassword(text);
			updateStatus(null);
		}
	}
}
